function _$(id) {
	return document.getElementById(id);
}
function $V(id) {
	return _$(id).val();
}

function getKeyCode(ev) {
	if (window.event)
		return window.event.keyCode;
	return ev.keyCode;
}
function drawStream(data) {
	var x = new Uint8Array(data, 13, len);
	var str = String.fromCharCode.apply(null, x);
	var canvas = document.getElementById("myCanvas");
	var ctx = canvas.getContext("2d");
	// ctx.fillStyle="green";
	// ctx.fillRect(10,10,124,50);
	// total size: imageWidth * imageHeight * 4; color format BGRA
	var bytes = new Uint8Array(data);
	var image = ctx.createImageData(canvas.width, canvas.height);
	for (var i = 0; i < bytes.length; i++) {
		image.data[i] = bytes[i + 13];
	}
	ctx.putImageData(image, 0, 0);
}

function drawBase64(data) {
	var c = document.getElementById("myCanvas");
	var ctx = c.getContext("2d");
	var img = new Image();
	img.src = "data:image/png;base64," + data;
	img.onload = function() {
		ctx.drawImage(img, 0, 0);
	}
}

function drawBase64EncodeInSer(data,offset,len, img) {
	var x = new Uint8Array(data, offset, len);
	var str = String.fromCharCode.apply(null, x);
	img.src = "data:image/png;base64," + str;
}

function drawBase64EncodeInClient(data,offset,len, img) {
	var x = new Uint8Array(data, offset, len);
	var str = String.fromCharCode.apply(null, x);
	img.src = "data:image/png;base64," + window.btoa(str);
//	var binary = "";
//	var bytes = new Uint8Array(data);
//	var len = bytes.byteLength;
//	for (var i = 0; i < len; i++) {
//		binary += String.fromCharCode(bytes[i + offset]);
//	}
//	img.src = "data:image/png;base64," + window.btoa(binary);
}

var Transport = {
	init : function(cfg) {
		var onMsgFun = this._onmessage;
		var host = window.location.host;
		var location = "ws://" + host + cfg.path;

		if (cfg['onmessage'])
			onMsgFun = cfg['onmessage'];
		if (cfg['host'] && cfg['port'])
			location = "ws://" + cfg['host'] + ":" + cfg['port'] + cfg.path;

		this._ws = new WebSocket(location);
		this._ws.status_div = cfg['status'];
		this._ws.binaryType = "arraybuffer";
		this._ws.onmessage = onMsgFun;
		this._ws.onopen = this._onopen;
		this._ws.onclose = this._onclose;
	},
	build : function(from, to, type,body) {
		var len = body.length;
		var arr = new ArrayBuffer(len + 13);
		var dv = new DataView(arr);
		dv.setInt8(0, type);
		dv.setInt32(1, len);
		dv.setInt32(5, from);
		dv.setInt32(9, to);
		for (var i = 0; i < len; i++) {
			dv.setInt8(i + 13, body.charCodeAt(i));
		}
		return dv;
	},
	send : function(msg) {
		if (!msg)
			return;
		if (typeof (msg) == "string" && msg.length == 0)
			return;
		try {
			this._ws.send(msg);
		} catch (e) {
			alert(e);
		}
	},
	// ------private method------------
	_onopen : function() {
		try {
			_$(this.status_div).innerHTML = 'Channel is open';
		} catch (e) {
			alert(e);
		}
	},
	_onmessage : function(m) {
		console.log(m.data);
	},
	_onclose : function(m) {
		this._ws = null;
	}
};